<script>
export default {
  name: 'RoleEdit',
  data () {
    return {
      show: false,
      dataForm: {},
      rules: {
        roleName: [{ required: true, message: '角色名不能为空' }]
      }
    }
  },
  methods: {
    getById (id) {
      this.show = true
      /* 添加和修改都对表单数据进行一次清空，这样可以去掉修改后再点添加按钮的缓存数据 */
      /* 在dialog组件上使用v-if对其进行判断，可以去掉修改后再点添加按钮的验证缓存 */
      this.dataForm = { ...this.$options.data().dataForm }
      if (id) {
        this.$axios.get('role',
          {
            params: {
              param: 'info',
              id
            }
          }
        ).then(data => {
          this.dataForm = { ...data }
        }).catch(e => e)
      }
    },
    submit () {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$axios.post('role?param=update', this.dataForm).then(() => {
            this.$message.success('更新成功！')
            this.show = false
            this.$emit('close')
          }).catch(e => e)
        }
      })
    }
  }
}
</script>

<template>
  <el-dialog title="角色编辑" :visible.sync="show" v-if="show">
    <el-form :model="dataForm" :rules="rules" label-width="80px" ref="dataForm">
      <el-form-item label="角色名" prop="roleName">
        <el-input v-model="dataForm.roleName" placeholder="请输入角色名" clearable/>
      </el-form-item>
      <el-form-item label="介绍">
        <el-input type="textarea" v-model="dataForm.profile" placeholder="请输入角色介绍" :rows="4" maxlength="200" show-word-limit/>
      </el-form-item>
      <el-form-item>
        <el-button @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
